<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="猪舍编码" prop="phouseCode">
        <el-input
          v-model="queryParams.phouseCode"
          placeholder="请输入猪舍编码"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="猪舍类型" prop="phouseType">
        <el-select
          v-model="queryParams.phouseType"
          placeholder="猪舍类型"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.hmy_park_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="负责人" prop="queryParams.staffName">
        <el-input
          v-model="queryParams.staffName"
          placeholder="请输负责人"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--      <el-form-item label="猪舍大小">-->
      <!--        <el-input-number-->
      <!--          :min="0"-->
      <!--          :max="9999"-->
      <!--          v-model="queryParams.beganSize"-->
      <!--          class="priceInput"-->
      <!--          style="width: 100px; margin-right: 5px"-->
      <!--        />-->
      <!--        - -->
      <!--        <el-input-number-->
      <!--          :min="0"-->
      <!--          :max="9999"-->
      <!--          v-model="queryParams.endSize"-->
      <!--          class="priceInput"-->
      <!--          ref="prodEnd"-->
      <!--          style="width: 100px; margin-left: 5px"-->
      <!--        />-->
      <!--      </el-form-item>-->

      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['hmy:park:save']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['hmy:park:save']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['hmy:park:del']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:role:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="phouseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" align="center" width="55"/>
      <el-table-column label="猪舍编码" align="center" prop="phouseCode" width="120"/>
      <el-table-column label="猪舍类型" align="center" prop="phouseType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.hmy_park_type" :value="scope.row.phouseType"/>
        </template>
      </el-table-column>
      <el-table-column label="负责人" align="center" prop="hmyStaff.staffName" width="70">
        <template slot-scope="scope">
          {{ scope.row.hmyStaff.staffName || '-' }}
        </template>
      </el-table-column>

      <el-table-column label="猪舍备注" align="center" prop="remark" width="100" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{ scope.row.remark || '-' }}
        </template>
      </el-table-column>
      <el-table-column label="猪舍大小" align="center" prop="phouseSize" :show-overflow-tooltip="true" width="100"/>
      <el-table-column label="环境监控" align="center" prop="phouseWenshi" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.hmy_presence_absence" :value="scope.row.phouseWenshi"/>
        </template>
      </el-table-column>
      <el-table-column label="气体检测仪" align="center" prop="phouseGas" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.hmy_presence_absence" :value="scope.row.phouseGas"/>
        </template>
      </el-table-column>
      <el-table-column label="气体交换机" align="center" prop="phouseFans" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.hmy_presence_absence" :value="scope.row.phouseFans"/>
        </template>
      </el-table-column>
      <el-table-column label="自动饲喂机" align="center" prop="phouseAutofeeding" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.hmy_presence_absence" :value="scope.row.phouseAutofeeding"/>
        </template>
      </el-table-column>
      <el-table-column label="刮粪机" align="center" prop="phouseScrape" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.hmy_presence_absence" :value="scope.row.phouseScrape"/>
        </template>
      </el-table-column>
      <el-table-column label="猪舍监控" align="center" prop="phouseMonitor" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.hmy_presence_absence" :value="scope.row.phouseMonitor"/>
        </template>
      </el-table-column>
      <!--      <el-table-column label="创建人" align="center" prop="createdBy" width="100"/>
            <el-table-column label="创建时间" align="center" prop="creationDate" width="180">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.creationDate) }}</span>
              </template>
            </el-table-column>
            <el-table-column label="修改人" align="center" prop="updatedBy" width="100">
              <template slot-scope="scope">
                {{ scope.row.updatedBy || '-' }}
              </template>
            </el-table-column>
            <el-table-column label="修改时间" align="center" prop="updationDate" width="180">
              <template slot-scope="scope">
                <span v-if="scope.row.updationDate">{{ parseTime(scope.row.updationDate) }}</span>
                <span v-else>-</span>
              </template>
            </el-table-column>-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['hmy:park:save']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['hmy:park:del']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改角色配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="猪舍编码" prop="phouseCode">
              <el-input v-model="form.phouseCode" placeholder="请输入猪舍编码"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="猪舍类型" prop="phouseType">
              <el-select
                v-model="form.phouseType"
                placeholder="猪舍类型"
                clearable
              >
                <el-option
                  v-for="dict in dict.type.hmy_park_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="负责人" prop="phouseManager">
              <el-select v-model="form.phouseManager" filterable placeholder="选择负责人" clearable>

                <el-option
                  v-for="item in hmyStaffList"
                  :key="item.staffId"
                  :label="item.staffName"
                  :value="item.staffId"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="猪舍面积" prop="phouseSize">
              <el-input-number v-model="form.phouseSize" controls-position="right"
                               :min="1" :max="9999"
                               placeholder="请输入猪舍面积"
                               style="width: 180px"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="备注">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 分配角色数据权限对话框 -->

  </div>
</template>

<script>
import {addPhouse, delPhousr, findCode, getPhouse, listPhouse, updatePhousr} from "@/api/hmy/phouse";
import {listStaff} from "@/api/hmy/staff";

export default {
  name: "Role",
  dicts: ['sys_normal_disable', 'hmy_park_type', 'hmy_presence_absence'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 角色表格数据
      phouseList: [],
      //获取员工列表
      hmyStaffList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示弹出层（数据权限）
      openDataScope: false,
      menuExpand: false,
      menuNodeAll: false,
      deptExpand: true,
      deptNodeAll: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        phouseCode: undefined,
        phouseType: undefined,
        staffName: undefined,
        beganTime: undefined,
        endTime: undefined,
        phouseManager: 0
      },
      // 表单参数
      form: {},
      //栏舍编码备份
      copyPhouseCoud: undefined,
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 表单校验
      rules: {
        phouseCode: [
          {required: true, message: "猪舍编码不能为空", trigger: "blur"},
          {validator: this.checkCode, trigger: 'blur'}
        ],
        phouseManager: [
          {required: true, message: "负责人不能为空", trigger: "blur"}
        ],
        phouseType: [
          {required: true, message: "猪舍类型不能为空", trigger: "blur"}
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /*栏舍编码重复校验*/
    checkCode(rule, value, callback) {
      findCode(value).then(response => {
        if (this.form.phouseId == undefined) {
          if (response.data != undefined) {
            callback(new Error("栏舍编码重复"))
          } else {
            callback()
          }
        }
        if (this.form.phouseId != undefined) {
          if (this.copyPhouseCoud != value && response.data != undefined) {
            callback(new Error("栏舍编码重复"))
          } else {
            callback()
          }
        }
        this.copyPhouseCoud = undefined;
      });
    },
    /** 查询角色列表 */
    getList() {
      this.loading = true;
      this.queryParams.beganTime = this.dateRange[0]
      this.queryParams.endTime = this.dateRange[1]
      listPhouse(this.queryParams).then(response => {
          this.phouseList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消按钮（数据权限）
    cancelDataScope() {
      this.openDataScope = false;
      this.reset();
    },
    // 表单重置
    reset() {
      if (this.$refs.menu != undefined) {
        this.$refs.menu.setCheckedKeys([]);
      }
      this.menuExpand = false,
        this.menuNodeAll = false,
        this.deptExpand = true,
        this.deptNodeAll = false,
        this.form = {
          phouseId: undefined,
          phouseCode: undefined,
          phouseType: undefined,
          phouseManager: null,
          staffId: undefined
        };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.staffName = undefined
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.phouseId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },

    /** 新增按钮操作 */
    handleAdd() {

      this.reset();
      this.open = true;
      this.title = "添加栏舍";
      listStaff(null).then(response => {
        this.hmyStaffList = response.rows;
        this.loading = false;
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const phouseId = row.phouseId || this.ids
      listStaff(null).then(response => {
        this.hmyStaffList = response.rows;
        this.loading = false;
      });
      getPhouse(phouseId).then(response => {
        this.form = response.data;
        this.copyPhouseCoud = this.form.phouseCode;
        this.open = true;
        this.title = "修改栏舍信息";
      });
    },

    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.phouseId != undefined) {
            updatePhousr(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPhouse(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 提交按钮（数据权限） */
    submitDataScope: function () {
      if (this.form.roleId != undefined) {
        this.form.deptIds = this.getDeptAllCheckedKeys();
        dataScope(this.form).then(response => {
          this.$modal.msgSuccess("修改成功");
          this.openDataScope = false;
          this.getList();
        });
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const phouseIds = row.phouseId || this.ids;
      this.$modal.confirm('是否确认删除角色编号为"' + phouseIds + '"的数据项？').then(function () {
        return delPhousr(phouseIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/role/export', {
        ...this.queryParams
      }, `role_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
